package com.feixiang.feixiangagent.rag;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import static org.springframework.ai.vectorstore.pgvector.PgVectorStore.PgDistanceType.COSINE_DISTANCE;
import static org.springframework.ai.vectorstore.pgvector.PgVectorStore.PgIndexType.HNSW;
    @Configuration
    public class PgVectorVectorStoreConfig {
        /**
         * 创建向量数据库PGVector
         * 1. 进入psql
         */
        //暂时注释
//        @Bean
//        public VectorStore pgVectorVectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel dashscopeEmbeddingModel) {
//            VectorStore vectorStore = PgVectorStore.builder(jdbcTemplate, dashscopeEmbeddingModel)
//                    .dimensions(1536)                    // Optional: defaults to model dimensions or 1536
//                    .distanceType(COSINE_DISTANCE)       // Optional: defaults to COSINE_DISTANCE
//                    .indexType(HNSW)                     // Optional: defaults to HNSW
//                    .initializeSchema(true)              // Optional: defaults to false
//                    .schemaName("public")                // Optional: defaults to "public"
//                    .vectorTableName("vector_store")     // Optional: defaults to "vector_store"
//                    .maxDocumentBatchSize(10000)         // Optional: defaults to 10000
//                    .build();
//            return vectorStore;
//        }
//    @Resource
//    private LoveAppDocumentLoader loveAppDocumentLoader;
    @Bean
    public VectorStore pgVectorVectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel dashscopeEmbeddingModel) {
        VectorStore vectorStore = PgVectorStore.builder(jdbcTemplate, dashscopeEmbeddingModel)
                .dimensions(1536)                    // Optional: defaults to model dimensions or 1536
                .distanceType(COSINE_DISTANCE)       // Optional: defaults to COSINE_DISTANCE
                .indexType(HNSW)                     // Optional: defaults to HNSW
                .initializeSchema(true)              // Optional: defaults to false
                .schemaName("public")                // Optional: defaults to "public"
                .vectorTableName("vector_store")     // Optional: defaults to "vector_store"
                .maxDocumentBatchSize(10000)         // Optional: defaults to 10000
                .build();
//        // 加载文档
//        List<Document> documents = loveAppDocumentLoader.loadMarkdowns();
//        vectorStore.add(documents);
        return vectorStore;
    }
}

